package com.quarkus.resource;

import cn.hutool.core.io.FileUtil;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.smallrye.jwt.build.Jwt;
import org.eclipse.microprofile.jwt.Claims;

import java.io.File;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @version v1.0
 * @Description: TODO(一句话描述该类的功能)
 * @Author: TongRui乀
 * @Date: 2021/3/7 9:55 上午
 */
public class GenerateTokenResource {

    public static void main(String[] args) {

        test_01();

    }

    public static void test_01(){
        var token = Jwt.issuer("https://example.com/issuer")
                .upn("jdoe@quarkus.io")
                .groups(new HashSet<>(Arrays.asList("User", "Admin")))
                .claim(Claims.birthdate.name(), "2021-03")
                .sign("privateKey.pem");

        System.out.println(token);
    }

    public static void test_02(){
        var pub = FileUtil.readString(new File("/Users/gengmei/IdeaProjects/getting-start-quarkus/getting-started-jwt/src/test/resources/privateKey.pem"), "utf-8");

        HashMap<String, Object> claims = new HashMap<>();

        claims.put("MP-JWT specific unique principal name", "jdoe@quarkus.io");
        claims.put("Birthday", "2021-03");
        claims.put("MP-JWT specific groups permission grant", new HashSet<>(Arrays.asList("User", "Admin")));

        System.out.println(Jwts.builder()
                .setIssuer("https://example.com/issuer")
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256, Base64.getEncoder().encodeToString(pub.getBytes()))
                .compact());
    }
}
